﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class ProductCategoryEntry : System.Web.UI.Page
    {
        private ProductCategoryManager aProductCategoryManager= new ProductCategoryManager();
        private ProductCategory aProductCategory= new ProductCategory();
        ProductGenderManager aProductGenderManager= new ProductGenderManager();
        ProductGender aProductGender=new ProductGender();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if(!IsPostBack)
                {
                    LoadGridview();

                    GenderNameDDList.DataSource = aProductGenderManager.GetAllProductGender();
                    GenderNameDDList.DataTextField = "genderName";
                    GenderNameDDList.DataValueField = "genderId";
                    GenderNameDDList.DataBind();
                    GenderNameDDList.Items.Insert(0, "--Select--");
                }          
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SaveProductCategory(object sender, EventArgs e)
        {
            try
            {
                aProductCategoryManager = new ProductCategoryManager();
                aProductCategory.CategoryName = categoryNameTextBox.Value;
                aProductGender.GenderId = Convert.ToInt32(GenderNameDDList.Value);
                aProductCategory.AProductGender = aProductGender;
                
                string message = aProductCategoryManager.SaveProductCategory(aProductCategory);
                messageResult.InnerText = message;
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Product category can not be saved" + "\n" + exception.ToString();
            }
        }

        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            categoryNameTextBox.Value = "";
        }

        //public void DeleteButton(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        int StoreId = Convert.ToInt32(storeIdTextBox.Value);
        //        StoreManager storeManager = new StoreManager();
        //        string aa = storeManager.DeleteStore(StoreId);
        //        messageResult.InnerText = aa;
        //    }
        //    catch (Exception)
        //    {
        //        messageResult.InnerText = "Delete Failed";
        //    }
        //}

        private void LoadGridview()
        {
            try
            {
                List<ProductCategory> aProductCategories;
                aProductCategories = aProductCategoryManager.GetAllProductCategory();
                categoryGridView.DataSource = aProductCategories;
                categoryGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadGridViewOnSearch(int i, string categoryIdorName)
        {
            try
            {
                categoryGridView.DataSource = null;
                categoryGridView.DataBind();
                List<ProductCategory> productCategories;
                ProductCategoryManager productCategoryManager = new ProductCategoryManager();
                productCategories = productCategoryManager.GetAllCategoryByIdOrName(i, categoryIdorName);
                categoryGridView.DataSource = productCategories;
                categoryGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void searchByIdButton_Click(object sender, EventArgs e)
        {
            int i = 0;
            LoadGridViewOnSearch(i, searchIdTextBox.Value);
        }
        protected void searchByNameButton_Click(object sender, EventArgs e)
        {
            int i = 1;
            LoadGridViewOnSearch(i, searchNameTextBox.Value);
        }
        protected void storeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            categoryGridView.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

    }
}